//
//  LXAlertTableViewController.m
//  bgms_d
//
//  Created by lachesis-zhu on 2019/3/15.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXAlertTableViewController.h"
#import "LXAlertTableViewCell.h"
#import "LXFoodItem.h"
#import "LXDevSettingModel.h"
#import "LXAlertTableViewController.h"

@interface LXAlertTableViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *alertArray;

@end

@implementation LXAlertTableViewController

#pragma mark -  public method

#pragma mark -  life cycle
- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializationData];
    [self addNotification];
    
    [self loadUI];
    
    [self loadViewDatas];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    
}

#pragma mark -  over write

#pragma mark -  setter & getter

#pragma mark -  private method
- (void)initializationData
{
    NSArray *array = [self getObjectForKey:@"alertData"];
    self.alertArray = [[NSMutableArray alloc] init];
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dic = obj;
        if ([dic[@"setting"] isEqualToString:@"YES"]) {
            [self.alertArray addObject:dic];
        }
    }];
    [self.tempArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        LXFoodItem *item = obj;
        if (item.lastAlertDate != nil) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            [dic setObject:@"NO" forKey:@"alert"];
            [dic setObject:@"YES" forKey:@"setting"];
            [dic setObject:NSLocalizedString(@"TemperatureEnded", nil) forKey:@"titleString"];
            NSDateFormatter *formate = [[NSDateFormatter alloc] init];
            [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *date = [formate stringFromDate:item.lastAlertDate];
            [dic setObject:date forKey:@"alertTime"];
            [dic setObject:[NSString stringWithFormat:@"%@#%ld %@:%@", NSLocalizedString(@"Probe", nil), item.placeNum + 1, NSLocalizedString(@"Temperature", nil), [[LXDevSettingModel shareSettingModel] tempCTempF:item.setNum]] forKey:@"alertedName"];
            [self.alertArray addObject:dic];
         }
    }];
}

- (void)loadUI
{
    self.view.backgroundColor = kAppBackgroundColor;
    [self buildNavi];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXAlertTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (self.alertArray.count > 0) {
        self.warnLabel.hidden = YES;
        self.warnImageV.hidden = YES;
        self.tableView.hidden = NO;
    } else {
        self.warnLabel.hidden = NO;
        self.warnImageV.hidden = NO;
        self.tableView.hidden = YES;
    }
}

- (void)loadViewDatas
{
    
}

- (void)buildNavi
{

}

#pragma mark -  notification
- (void)addNotification
{
    
}

#pragma mark -  network


#pragma mark -  event handle

#pragma mark -  delegate
#pragma mark UITableViewDelegate & UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [LXAutoLength lx_L:94];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.alertArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LXAlertTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell drawAroundViewWithLocation:DrawBottom color:kAppLinesColor insets:UIEdgeInsetsMake(0, 0, 0.5, 0)];
    NSDictionary *dic = [self.alertArray objectAtIndex:indexPath.row];
    cell.timeLabel.text = [dic[@"alertTime"] substringWithRange:NSMakeRange(11, 5)];
    cell.detailLabel.text = [NSString stringWithFormat:@"%@", dic[@"alertedName"]];
    cell.subDetailLabel.text = dic[@"titleString"];
    if ([dic[@"alert"] isEqualToString:@"NO"]) {
        cell.detailLabel.textColor = kAppDeepLineColor;
        cell.subDetailLabel.textColor = kAppDeepLineColor;
        cell.imageWidth.constant = 0;
        cell.warnImage.image = [UIImage imageNamed:@""];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}

#pragma mark -  lazy load


#pragma mark -  memoryWarning

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
